﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.Model;
using QuanLyHocPhi.BLL;

namespace QuanLyHocPhi.GUI.DuLieu
{
    public partial class FormSelectHocKy : Form
    {
        private string m_ExcludedMahocKy = null;

        public FormSelectHocKy(NamHoc namHocToFocus, string maHocKyToFocus, string excludedMaHocKy = null)
        {
            InitializeComponent();
            
            m_ExcludedMahocKy = excludedMaHocKy;

            m_NeedLoadHocKy = false;
            NapDanhSachNamHoc(namHocToFocus == null ? null : namHocToFocus.TenNamHoc);
            UpdateGuiBySelectedNamHoc();
            NapDanhSachHocKy(maHocKyToFocus);
            UpdateGuiBySelectedHocKy();
            m_NeedLoadHocKy = true;
        }

        #region SelectedNamHoc
        public NamHoc SelectedNamHoc
        {
            get
            {
                if (bsNamHoc.Current == null)
                    return null;
                else
                    return bsNamHoc.Current as NamHoc;
            }
        }
        #endregion

        #region SelectedHocKy
        public HocKy SelectedHocKy
        {
            get
            {
                if (bsHocKy.DataSource == null || bsHocKy.Current == null)
                    return null;
                else
                    return (bsHocKy.Current as HocKy);
            }
        }

        private string SelectedMaHocKy
        {
            get
            {
                var hocky = this.SelectedHocKy;
                if (hocky == null)
                    return null;
                else
                    return hocky.MaHocKy;
            }
        }
        #endregion


        private bool m_NeedLoadHocKy = true;
        private void NapDanhSachNamHoc(string tenNamHocToFocus = null)
        {
            cbxNamHoc.DataSource = null;
            var lstNamHoc = BizNamHoc.GetListNamHoc();
            bsNamHoc.DataSource = lstNamHoc;

            if (tenNamHocToFocus != null)
            {
                for (int i = 0; i < lstNamHoc.Count; i++)
                    if (lstNamHoc[i].TenNamHoc == tenNamHocToFocus)
                    {
                        bsNamHoc.Position = i;
                        break;
                    }
            }
            cbxNamHoc.DataSource = bsNamHoc;
        }

        private void NapDanhSachHocKy(string maHocKyToFocus = null)
        {
            cbxHocKy.DataSource = null;
            var namHoc = this.SelectedNamHoc;
            if (namHoc == null)
                bsHocKy.DataSource = null;
            else
            {
                var lstHocKy = BizHocKy.GetList_HocKy(namHoc.TenNamHoc);
                for (int i = lstHocKy.Count - 1; i >= 0; i--)
                    if (lstHocKy[i].MaHocKy == m_ExcludedMahocKy)
                        lstHocKy.RemoveAt(i);
                bsHocKy.DataSource = lstHocKy;
                for (int i = 0; i < lstHocKy.Count; i++)
                    if (lstHocKy[i].MaHocKy == maHocKyToFocus)
                    {
                        bsHocKy.Position = i;
                        break;
                    }
            }
            cbxHocKy.DisplayMember = "TenHocKy";
            cbxHocKy.DataSource = bsHocKy;
        }

        #region OnSelectedNamHocChanged
        private void bsNamHoc_CurrentChanged(object sender, EventArgs e)
        {
            UpdateGuiBySelectedNamHoc();
            if (m_NeedLoadHocKy)
                NapDanhSachHocKy();
        }

        private void UpdateGuiBySelectedNamHoc()
        {
            var namHoc = this.SelectedNamHoc;
            if (namHoc == null)
            {
                cbxHocKy.Enabled = false;
            }
        }
        #endregion

        #region OnSelectedHocKyChanged
        private void bsHocKy_CurrentChanged(object sender, EventArgs e)
        {
            UpdateGuiBySelectedHocKy();
        }

        private void UpdateGuiBySelectedHocKy()
        {
            var hocky = this.SelectedHocKy;
            if (hocky == null)
            {
                btnOK.Enabled = false;
            }
            else
            {
                btnOK.Enabled = true;
            }
        }
        #endregion
    }
}
